/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package mephisto.rest;

import java.util.logging.Level;

import org.restlet.Context;
import org.restlet.Filter;
import org.restlet.data.Request;
import org.restlet.data.Response;

import com.noelios.restlet.ext.servlet.ServletCall;
import com.noelios.restlet.http.HttpCall;
import com.noelios.restlet.http.HttpRequest;
import com.noelios.restlet.http.HttpResponse;

/**
 * This is a filter that setups the Mephisto context before servicing a request
 * and cleans things up after the request has been serviced.
 */

public class LifecycleFilter extends Filter {

    public LifecycleFilter(Context context) {
        super(context);
    }

    /**
     * Initialize the XWiki context and put relevant objects in the Restlet
     * context attributes.
     */
    @Override
    protected int beforeHandle(Request request, Response response) {
        Context xwikiContext = null;
        
        try {
            //xwikiContext = com.xpn.xwiki.web.Utils.prepareContext("", xwikiRequest, xwikiResponse,
            //        xwikiServletContext);

            //initializeContainerComponent(xwikiContext);
            //XWiki xwiki = XWiki.getXWiki(xwikiContext);

            /*
             * XWiki platform objects are stocked in the Restlet context so all
             * Restlet components can retrieve them.
             */
            //Map<String, Object> attributes = getContext().getAttributes();
            //attributes.put(MephistoConstants.CONTEXT, xwikiContext);
            //attributes.put(MephistoConstants.MEPHISTO, mephisto);
            
        } catch (Exception e) {
            e.printStackTrace();
//            if (xwikiContext != null) {
//                cleanupComponents();
//            }

            getLogger().log(Level.SEVERE, "Error while initializing context.");

            /*
             * In case of exception do not relay the request to components
             * behind this filter.
             */
            return Filter.STOP;
        }

        getLogger().log(Level.FINE, "Context initialized.");

        return Filter.CONTINUE;
    }

    @Override
    protected void afterHandle(Request request, Response response) {
        //Utils.cleanupResource(request, componentManager, getLogger());

        //Context xwikiContext = (XWikiContext) getContext().getAttributes().get(Constants.XWIKI_CONTEXT);
        //if (xwikiContext != null) {
          //  getLogger().log(Level.FINE, "XWiki context cleaned up.");
            //cleanupComponents();
        //}

        super.afterHandle(request, response);
    }

    /**
     * Builds the servlet request.
     * 
     * @param req
     *            The request to handle.
     * @return httpServletRequest The http servlet request.
     */
/*    protected static HttpServletRequest getHttpRequest(Request req) {
        if (req instanceof HttpRequest) {
            HttpRequest httpRequest = (HttpRequest) req;
            HttpCall httpCall = httpRequest.getHttpCall();
            if (httpCall instanceof ServletCall) {
                return ((ServletCall) httpCall).getRequest();
            }
        }
        return null;
    }
*/
    /**
     * Builds the servlet response.
     * 
     * @param res
     *            The response.
     * @return httpServletResponse The http servlet response.
     */
/*    protected static HttpServletResponse getHttpResponse(Response res) {
        if (res instanceof HttpResponse) {
            HttpResponse httpResponse = (HttpResponse) res;
            HttpCall httpCall = httpResponse.getHttpCall();
            if (httpCall instanceof ServletCall) {
                return ((ServletCall) httpCall).getResponse();
            }
        }
        return null;
    }
*/

}
